#include "QtClock.h"
#include<QPainter>
#include<QPen>
#include<QTime>
#include<QTimer>





QtClock::QtClock(QWidget *parent)
    : QWidget(parent)
{
    
    ui.setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    resize(600,600);
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    timer->start(1000);
}

QtClock::~QtClock()
{}

void QtClock::paintEvent(QPaintEvent *)
{
    static const QPoint hour[3] = {
        QPoint(14,15),
        QPoint(-14, 15),
        QPoint(0,-150)
    };
    static const QPoint min[3] = {
        QPoint(11,13),
        QPoint(-11, 13),
        QPoint(0,-170)
    };
    static const QPoint sec[3] = {
        QPoint(7,8),
        QPoint(-7, 8),
        QPoint(0,-210)
    };

    int size = qMin(height(), width());
    QTime time = QTime::currentTime();
    QPainter painter = QPainter(this);
    QPainter* p = &painter;
    p->setRenderHint(QPainter::Antialiasing);
    
    p->translate(width()/2,height()/2);
    p->scale(size/600,size/600);

    QBrush brush;
    brush.setColor(QColor(245,182,95));
    brush.setStyle(Qt::SolidPattern);

    QPen pen;
    pen.setWidth(18);
    pen.setColor(QColor(205,214,216));
    pen.setStyle(Qt::SolidLine);
    p->setPen(pen);
    
    p->drawEllipse(QPoint(0,0),280,280);
    pen.setColor(Qt::white);
    pen.setWidth(160);
    p->setPen(pen);
    p->drawEllipse(QPoint(0,0),160,160);
    p->setBrush(brush);


    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    pen.setColor(Qt::cyan);
    p->setPen(pen);
    for (size_t i = 0; i < 12; i++)
    {
        p->drawLine(0,268,0,276);
        QFont font = painter.font();
        font.setPixelSize(32);
        painter.setFont(font);
        if (i==0)
        {

            p->drawText(-15, -230, QString::number(12));
        }
        else
        {

            p->drawText(-15, -230, QString::number(i));
        }
        p->rotate(30);
    }

    p->setBrush(brush);
    p->setPen(Qt::NoPen);
    p->save();
    p->rotate(30.0*(time.hour()+time.minute()/60));
    p->drawConvexPolygon(hour,3);
    p->restore();


    p->setPen(Qt::NoPen);
    p->setBrush(QColor(129, 117, 176));
    p->save();
    p->rotate(6*time.minute());
    p->drawConvexPolygon(min, 3);
    p->restore();




    p->setPen(Qt::NoPen);
    p->setBrush(QColor(119, 217, 176));
    p->save();
    p->rotate(6 * time.second());
    p->drawConvexPolygon(sec, 3);
    p->restore();


}
